package com.rc.saas.tenant.service.member;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.constant.WebConstant;
import com.rc.saas.tenant.mapper.member.MemberInfoMapper;
import com.rc.saas.tenant.mapper.member.MemberMultchargeApplyMapper;
import com.rc.saas.tenant.model.member.MemberInfo;
import com.rc.saas.tenant.model.member.MemberMultchargeApply;
import com.rc.saas.tenant.search.member.MemberMultchargeApplySearch;
import com.rc.saas.tenant.vo.member.MemberMultchargeApplyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by sven on 2018-04-17
 */
@Service
public class MemberMultchargeApplyServiceImpl implements MemberMultchargeApplyService {
    @Autowired
    private MemberMultchargeApplyMapper memberMultchargeApplyMapper;

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Override
    public MemberMultchargeApplyMapper getMapper() {
        return memberMultchargeApplyMapper;
    }

    @Override
    public void updateApprovalById(Long applyId, String status) {
        MemberMultchargeApply memberMultchargeApply = memberMultchargeApplyMapper.selectByPrimaryKey(applyId);

        MemberMultchargeApply updateApply = new MemberMultchargeApply();
        updateApply.setApplyId(applyId);
        //审核类型 0申请一号多充 1申请取消
        updateApply.setApplyType(memberMultchargeApply.getApplyType());
        //审核状态 0未审核，1审核通过，2审核不通过 默认0
        if (("1").equals(status)) {
            updateApply.setStatus("1");
        } else if ("2".equals(status)) {
            updateApply.setStatus("2");
        }

        updateApply.setAuditingTime(new Date());
        memberMultchargeApplyMapper.updateByPrimaryKeySelective(updateApply);

        //审核通过则设置为多充会员，默认50元/车;
        MemberMultchargeApply findApply = memberMultchargeApplyMapper.selectByPrimaryKey(applyId);
        if (("1").equals(status)) {
            if (findApply != null) {
                MemberInfo updateMember = new MemberInfo();
                updateMember.setMemberId(findApply.getMemberId());

                //(elec_type)充电类型 0单冲  1多充 默认0
                updateMember.setChargeType("1");
                updateMember.setMultChargePrice(new BigDecimal("50"));
                memberInfoMapper.updateByPrimaryKeySelective(updateMember);
            }
        }
    }

    @Override
    public void updateCancelApprovalById(Long applyId) {
        MemberMultchargeApply updateRecord = new MemberMultchargeApply();
        updateRecord.setApplyId(applyId);
        //审核类型 0申请一号多充 1申请取消
        updateRecord.setApplyType(WebConstant.STATUS_ONE);
        //审核状态 0未审核，1审核通过，2审核不通过 默认0
        updateRecord.setStatus(WebConstant.STATUS_ONE);
        updateRecord.setAuditingTime(new Date());
        memberMultchargeApplyMapper.updateByPrimaryKeySelective(updateRecord);

        //取消审核则设置为单充会员;
        MemberMultchargeApply findApply = memberMultchargeApplyMapper.selectByPrimaryKey(applyId);
        if (findApply != null) {
            MemberInfo updateMember = new MemberInfo();
            updateMember.setMemberId(findApply.getMemberId());

            //(elec_type)充电类型 0单冲  1多充 默认0
            updateMember.setChargeType("0");
            updateMember.setMultChargePrice(new BigDecimal("0"));
            memberInfoMapper.updateByPrimaryKeySelective(updateMember);
        }
    }

    @Override
    public PageInfo<MemberMultchargeApplyResult> searchByCondition(MemberMultchargeApplySearch search) {
        PageHelper.startPage(search.getOffset(), search.getLimit());
        List<MemberMultchargeApplyResult> list = memberMultchargeApplyMapper.searchByCondition(search);
        return new PageInfo<MemberMultchargeApplyResult>(list);
    }
}
